﻿using Web.API.IBLL;
using Web.API.IDAL;
using Web.API.Model;

namespace Web.API.BLL
{
    public class UserBLL:UserIBLL
    {
        public UserBLL(UserIDAL<UserModel> dal, UserIDAL<UserStart> dAl) 
        {
            Dal = dal;
            DAl = dAl;
        }
        public UserIDAL<UserModel> Dal { get;}
        public UserIDAL<UserStart> DAl { get;}

        public Task<int> AddStart(UserStart start)
        {
           return DAl.AddAsync(start);
        }

        public Task<int> AddUser(UserModel user)
        {
            var s1 = DateTime.Now.ToString("yyyyMMddHHmmss");
            var s2=new Random().Next(100,999);
            var s3 = s1 + s2;
            
            return Dal.AddAsync(user);
        }

        public UserModel Fantian(int id)
        {
            return Dal.GetAll().FirstOrDefault(x=>x.Id==id);
        }

        public dynamic GetUser(int index, int size)
        {
            var list = from a in Dal.GetAll().Where(x => x.IsDelete == true)
                       join b in DAl.GetAll() on a.StartId equals b.StartId
                       select new
                       {
                           a.Id,
                           a.Name,
                           a.Pos,
                           a.BirthDay,
                           a.Age,
                           a.Img,
                           a.StartId,
                           a.Sex,
                          StartName= b.Name
                       };
            var c=list.Count();
            var d=list.OrderBy(x=>x.Id).Skip((index-1)*size).Take(size).ToList();
            return new
            {
                count=c,
                data=d
            };
        }

        public Task<int> UpdUser(UserModel user)
        {
           return Dal.UpdateAsync(user);
        }

        public List<UserStart> Xiala()
        {
           return DAl.GetAll().ToList();
        }
    }
}
